﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wjy自制小框架.Common.Mapper
{
    /// <summary>
    /// Sql条件构造器
    /// wjy
    /// </summary>
    class QueryWrapper
    {
        private List<string> where = new List<string>();
        private List<string> orderBy = new List<string>();

        public bool isEmpty(object val)
        {
            if (val == null) return true;
            if (val.ToString() == "") return true;
            return false;
        }

        public QueryWrapper likeIfPresent(string column, object val)
        {
            if (!isEmpty(val))
            {
                val = val.ToString().Replace("%", "\\%");
                where.Add($"`{ column }` like '%{ val }%'");
            }
            return this;
        }

        public QueryWrapper inIfPresent(string column, List<string> values)
        {
            if (values != null && values.Count > 0)
            {
                where.Add($"`{ column }` in ('{ string.Join("','", values) }')");
            }
            return this;
        }

        public QueryWrapper inIfPresent(string column, string[] values)
        {
            if (values != null && values.Length > 0)
            {
                where.Add($"`{ column }` in ('{ string.Join("','", values) }')");
            }
            return this;
        }

        public QueryWrapper eqIfPresent(string column, object val)
        {
            if (!isEmpty(val))
            {
                where.Add($"`{ column }` = '{ val }'");
            }
            return this;
        }

        public QueryWrapper neIfPresent(string column, object val)
        {
            if (!isEmpty(val))
            {
                where.Add($"`{ column }` != '{ val }'");
            }
            return this;
        }

        public QueryWrapper gtIfPresent(string column, object val)
        {
            if (!isEmpty(val))
            {
                where.Add($"`{ column }` > '{ val }'");
            }
            return this;
        }

        public QueryWrapper geIfPresent(string column, object val)
        {
            if (!isEmpty(val))
            {
                where.Add($"`{ column }` >= '{ val }'");
            }
            return this;
        }

        public QueryWrapper ltIfPresent(string column, object val)
        {
            if (!isEmpty(val))
            {
                where.Add($"`{ column }` < '{ val }'");
            }
            return this;
        }

        public QueryWrapper leIfPresent(string column, object val)
        {
            if (!isEmpty(val))
            {
                where.Add($"`{ column }` <= '{ val }'");
            }
            return this;
        }

        public QueryWrapper betweenIfPresent(string column, object val1, object val2)
        {
            if (!isEmpty(val1) && !isEmpty(val2))
            {
                where.Add($"`{ column }` >= '{ val1 }' and `{ column }` <= '{ val2 }'");
            }
            if (!isEmpty(val1))
            {
                return geIfPresent(column, val1);
            }
            if (!isEmpty(val2))
            {
                return leIfPresent(column, val2);
            }
            return this;
        }

        public QueryWrapper orderByAsc(string column)
        {
            orderBy.Add(column + " asc");
            return this;
        }

        public QueryWrapper orderByDesc(string column)
        {
            orderBy.Add(column + " desc");
            return this;
        }

        public string buildSql()
        {
            string Sql = "";
            if (where.Count > 0)
            {
                Sql = " where " + string.Join(" and ", where);
            }
            if (orderBy.Count > 0)
            {
                Sql += " order by " + string.Join(",", orderBy);
            }
            return Sql;
        }
    }
}
